/*
 * ServerEdit.java
 *
 * Created on 13 de agosto de 2008, 22:48
 */

package l2bot.interfaz.dialogs;

import com.sun.org.apache.xerces.internal.parsers.*;
import java.io.*;


import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import l2bot.Idioma;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

/**
 *
 * @author  carl
 */
public class ServerEdit extends javax.swing.JDialog {
    boolean nuevo;
    String na;
    String[] nombres;
    Document doc;
    /** Creates new form ServerEdit */
    public ServerEdit(java.awt.Frame parent, boolean modal, String name) {
        super(parent, modal);
        initComponents();
        
        DOMParser parser = new DOMParser();
        try {
            parser.parse("xml/servers.xml");
        } catch (SAXException ex) {

        } catch (IOException ex) {

        }
        doc = parser.getDocument();       
        
        if(name == null){//nuevo server
            nuevo = true;
            jTextField3.setEditable(true);
            this.setTitle("Nuevo servidor");
            jTextField1.setText("l2authd.lineage2.com");
            jTextField2.setText("2106");
            jTextField3.setText("Servidor oficial");
            jTextField4.setText("Bartz,Devianne,Teon,Franz,Luna,Sayha,Aria,Phoenix");
            jButton1.setText("Nuevo Servidor");
            
        }else{
            this.setTitle("Editar servidor " + name);
                    nuevo = false;
                    jTextField3.setEditable(false);
                    na = name;
                    NodeList f = doc.getElementsByTagName("server");
                    for(int i=0;i<f.getLength();i++){
                        Element h = (Element)f.item(i);
                        if(h.getAttribute("name").equals(name)){
                            jTextField1.setText(h.getAttribute("host"));
                            jTextField2.setText(h.getAttribute("port"));
                            jTextField3.setText(h.getAttribute("name"));
                            jTextField4.setText(h.getAttribute("gameservers"));
                            jButton1.setText("Guardar Servidor");
                        }
                    }
            
        }
        
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        label1 = new java.awt.Label();
        label2 = new java.awt.Label();
        jTextField2 = new javax.swing.JTextField();
        label3 = new java.awt.Label();
        jTextField3 = new javax.swing.JTextField();
        label4 = new java.awt.Label();
        jTextField4 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);
        setResizable(false);

        jButton1.setText(Idioma.aceptar);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextField1.setText("jTextField1");
        jTextField1.setMaximumSize(new java.awt.Dimension(60, 20));

        label1.setText(Idioma.host_ip_o_dominio + ":");

        label2.setText(Idioma.puerto + ":");

        jTextField2.setText("jTextField2");
        jTextField2.setMaximumSize(new java.awt.Dimension(60, 20));

        label3.setText(Idioma.nombre + ":");

        jTextField3.setText("jTextField3");
        jTextField3.setMaximumSize(new java.awt.Dimension(60, 20));

        label4.setText(Idioma.gameservers_separar_con_comas + ":");

        jTextField4.setText("jTextField4");
        jTextField4.setMaximumSize(new java.awt.Dimension(60, 20));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(102, 102, 102))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        
    if(nuevo && existe(jTextField3.getText())){
        JOptionPane.showConfirmDialog( this,"El nombre ya existe","Error",JOptionPane.WARNING_MESSAGE );
        return;
    }
    try {
            NodeList f = doc.getElementsByTagName("server");        
            FileWriter w = new FileWriter("xml/servers.xml", false);
            w.write("<servers>\n");
            for (int i = 0; i < f.getLength(); i++) {
                Element h = (Element) f.item(i);
                if(!nuevo && h.getAttribute("name").equals(na)){
                    w.write("     <server name='"+jTextField3.getText()+"' host='"+jTextField1.getText()+"' port='"+jTextField2.getText()+"' gameservers='"+jTextField4.getText()+"'/>\n");
                }else{
                    w.write("     <server name='"+h.getAttribute("name")+"' host='"+h.getAttribute("host")+"' port='"+h.getAttribute("port")+"' gameservers='"+h.getAttribute("gameservers")+"'/>\n");
                }
            }
            if(nuevo){
                w.write("     <server name='"+jTextField3.getText()+"' host='"+jTextField1.getText()+"' port='"+jTextField2.getText()+"' gameservers='"+jTextField4.getText()+"'/>\n");
            }
            
            w.write("</servers>");
            w.flush();
            w.close();
        } catch (IOException ex) {
            
        }
    setVisible(false);
}//GEN-LAST:event_jButton1ActionPerformed

boolean existe(String s)
{
    NodeList f = doc.getElementsByTagName("server");
    for(int i=0;i<f.getLength();i++){
        Element h = (Element)f.item(i);
        if(h.getAttribute("name").equals(s)){
            return true;
        }
    }
    return false;
}

    /**
    * @param args the command line arguments
    */
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private java.awt.Label label4;
    // End of variables declaration//GEN-END:variables

}
